﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Models;
using AnesSystem.Services;

namespace AnesSystem
{
    public abstract class EventDataCache
    {
        #region Variables
        private Dictionary<string, PatientEventsModel> _mapID = new Dictionary<string, PatientEventsModel>();
        private Dictionary<string, List<PatientEventsModel>> _mapCode2List = new Dictionary<string, List<PatientEventsModel>>();
        private Dictionary<string, string> _mapID2Code = new Dictionary<string, string>();
        protected PatientLinkModel _patientLink;


        #endregion


        #region Constructor
        public EventDataCache()
        {
           
        }

        #endregion


        #region Properties
        public Dictionary<string, PatientEventsModel> MapID
        {
            get
            {
                return _mapID;
            }

            set
            {
                _mapID = value;
            }
        }

        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods

        #endregion


        #region Public/ProtectedMethods
        /// <summary>
        /// 添加和更新事件
        /// </summary>
        /// <param name="patientEventsModel"></param>
        public void AddUpdateEvent(PatientEventsModel patientEventsModel)
        {
            if (!DataManager.Instance.Code2events.ContainsKey(patientEventsModel.Code))
            {
                DataManager.Instance.GetEventConfigByCode(patientEventsModel.Code);
            }
            //当前缓存无该事件
            if (!string.IsNullOrEmpty(patientEventsModel.Id.ToString()) && !MapID.ContainsKey(patientEventsModel.Id.ToString()))
            {
                MapID.Add(patientEventsModel.Id.ToString(), patientEventsModel);
            }
            //更新
            if (!MapID.ContainsKey(patientEventsModel.Id.ToString()))
            {
                MapID[patientEventsModel.Id.ToString()] = patientEventsModel;
            }

            string oldCode = "";
            if (patientEventsModel.Code == null) return;
            if (_mapID2Code.ContainsKey(patientEventsModel.Id.ToString()))
            {
                oldCode = _mapID2Code[patientEventsModel.Id.ToString()];
            }
            //无该事件(多次持续事件及第一次单次事件)
            if (string.IsNullOrEmpty(oldCode))
            {
                _mapID2Code.Add(patientEventsModel.Id.ToString(), patientEventsModel.Code);
                List<PatientEventsModel> list = null;
                if (_mapCode2List.ContainsKey(patientEventsModel.Code))
                {
                    list = _mapCode2List[patientEventsModel.Code];
                }

                if (list == null)
                {
                    list = new List<PatientEventsModel>();
                    _mapCode2List.Add(patientEventsModel.Code, list);
                }
                list.Add(patientEventsModel);
            }
            //存在该事件(更新)
            else
            {
                if (oldCode.Equals(patientEventsModel.Code))
                {
                    List<PatientEventsModel> list = _mapCode2List[oldCode];
                    if (list != null && list.Count > 0)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (list[i].Id.ToString().Equals(patientEventsModel.Id.ToString()))
                            {
                                list[i] = patientEventsModel;
                            }
                        }
                    }

                }
            }
        }

        public PatientEventsModel GetEventById(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                if (!MapID.ContainsKey(id))
                {
                    PatientEventsModel patientEvent = PatientEventsService.Instance.GetObjById(id);
                    if (patientEvent != null)
                    {
                        MapID.Add(id, patientEvent);
                    }
                }
                return MapID[id];

            }
            else
            {
                return null;
            }

        }

        public List<PatientEventsModel> SearchEventExeByName(string name)
        {
            List<PatientEventsModel> list = new List<PatientEventsModel>();
            foreach (var item in _mapCode2List.Keys)
            {
                ConfigEventsModel configEvent = DataManager.Instance.GetEventConfigByCode(item);
                if (configEvent.Name.Contains(name))
                {
                    list.AddRange(_mapCode2List[item]);
                }
            }
            return list;
        }

        public List<PatientEventsModel> SearchEventExeByCode(string code)
        {
            List<PatientEventsModel> list = new List<PatientEventsModel>();
            foreach (var item in _mapCode2List.Keys)
            {
                ConfigEventsModel configEvent = DataManager.Instance.GetEventConfigByCode(item);
                if (configEvent.Code.Equals(code))
                {
                    list.AddRange(_mapCode2List[item]);
                }
            }
            return list;
        }

        public List<PatientEventsModel> SearchAllEventExe()
        {
            List<PatientEventsModel> list = new List<PatientEventsModel>();
            foreach (var item in _mapCode2List.Keys)
            {
                list.AddRange(_mapCode2List[item]);
            }
            return list;
        }

        public void FindAllEventExe(Dictionary<string, List<PatientEventsModel>> allExeList)
        {
            allExeList = _mapCode2List;
        }
        public abstract void DbSearch();
       
        public void ClearCache()
        {
            MapID.Clear();
            _mapID2Code.Clear();
            _mapCode2List.Clear();
        }

        public void DelEvent(PatientEventsModel patientEventsModel)
        {
            if (string.IsNullOrEmpty(patientEventsModel.Id.ToString()))
            {
                return;
            }
            if (_mapID.ContainsKey(patientEventsModel.Id.ToString()))
            {
                _mapID.Remove(patientEventsModel.Id.ToString());
            }
            if (_mapID2Code.ContainsKey(patientEventsModel.Id.ToString()))
            {
                _mapID2Code.Remove(patientEventsModel.Id.ToString());
            }
            if (string.IsNullOrEmpty(patientEventsModel.Code)) return;
            if (_mapCode2List.ContainsKey(patientEventsModel.Code))
            {
                List<PatientEventsModel> list = _mapCode2List[patientEventsModel.Code];
                if (list != null && list.Count > 0)
                {
                    int index = -1;
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i].Id.ToString().Equals(patientEventsModel.Id.ToString()))
                        {
                            index = i;
                            break;
                        }
                    }
                    if (index != -1)
                    {
                        list.RemoveAt(index);
                    }
                }
            }
            PatientEventsService.Instance.DeleteById(patientEventsModel.Id);

        }


        #endregion


        #region EventHandlers


        #endregion
    }
}
